//
//  SQLiteManager.swift
//  weibopush
//
//  Created by 龚欣 on 15/7/14.
//  Copyright © 2015年 gong. All rights reserved.
//

import Foundation

class SQLiteManager {
    
    private static let instance = SQLiteManager()
    
    class var sharedManager : SQLiteManager {return instance}
    
    var queue : FMDatabaseQueue?
    
    func openDB(dbName : String){
        
        let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!.stringByAppendingPathComponent(dbName)
        queue = FMDatabaseQueue(path: path)
        
        createTable()
    }
    
    func createTable(){
        let path = NSBundle.mainBundle().pathForResource("tables.sql", ofType: nil)!
        
        let sql = try! String(contentsOfFile: path)
        
        queue?.inDatabase({ (db) -> Void in
            if db!.executeUpdate(sql){
                print("建表成功")
            }else{
                print("建表失败")
            }
        })
    }
}